//
//  JsonKits.swift
//  xiaoqiao
//
//  Created by 冷子豪 on 2017/8/5.
//  Copyright © 2017年 冷子豪. All rights reserved.
//

import UIKit

var jsonKits = JsonKits()

class JsonKits{
    
    /// 当前用户账户信息，转json
    ///
    /// - Returns: json 字符串
    func toJson(user:User) -> String{
        // 转换成字典
        var dic = [String:Any]()
        
        dic["id"] = user.id
        dic["nickname"] = user.nickname
        dic["password"] = user.password
        dic["realname"] = user.realname
        dic["sex"] = user.sex
        dic["phone"] = user.phone
        dic["province"] = user.province
        dic["city"] = user.city
        dic["district"] = user.district
        dic["email"] = user.email
        dic["cardholder"] = user.cardHolder
        dic["cardbank"] = user.cardBank
        dic["cardnumber"] = user.cardNumber
        
        if(user.birthday == nil) {
            dic["birthday"] = 0
        } else {
            dic["birthday"] = Int(user.birthday!.timeIntervalSince1970) * 1000
        }
        dic["icon"] = user.icon
        dic["grade"] = user.grade
        dic["status"] = user.status
        
//        if(user.createtime == nil) {
//            dic["createtime"] = 0
//        } else {
//            dic["createtime"] = Int(user.createtime!.timeIntervalSince1970) * 1000
//        }
//        
//        if(user.updatetime == nil) {
//            dic["updatetime"] = 0
//        } else {
//            dic["updatetime"] = Int(user.updatetime!.timeIntervalSince1970) * 1000
//        }
        //        print(dic)
        
        // 转换成json
        if (!JSONSerialization.isValidJSONObject(dic)) {
            print("无法解析出JSONString")
        }
        let data:NSData! = try? JSONSerialization.data(withJSONObject: dic, options: []) as NSData!
        let JSONString = NSString(data:data as Data,encoding: String.Encoding.utf8.rawValue)
        return JSONString! as String
    }
    
    
    /// 当前用户详细信息，转json
    ///
    /// - Returns: json 字符串
    func toJson(user:UserInfo) -> String{
        // 转换成字典
        var dic = [String:Any]()
        
        dic["id"] = user.id
        dic["userid"] = user.userid
        dic["sex"] = user.sex
        dic["age"] = user.age
        dic["cityofinfo"] = user.cityofinfo
        dic["income"] = user.income
        dic["incomeofhome"] = user.incomeOfHome
        dic["marriage"] = user.marriage
        dic["health"] = user.health
        dic["education"] = user.education
        dic["countofchildren"] = user.countOfChildren
        dic["house_id"] = user.house_id
        dic["vehicle_id"] = user.vehicle_id
        dic["companyofnature"] = user.companyOfNature
        
        if(user.startOfWork == nil) {
            dic["startofwork"] = 0
        } else {
            dic["startofwork"] = Int(user.startOfWork!.timeIntervalSince1970) * 1000
        }
        
        if(user.hiredate == nil) {
            dic["hiredate"] = 0
        } else {
            dic["hiredate"] = Int(user.hiredate!.timeIntervalSince1970) * 1000
        }
        
        dic["ageofwork"] = user.ageOfWork
        dic["position"] = user.position
        dic["professionalskill"] = user.professionalSkill
        dic["stateofbankofcount"] = user.stateOfbankOfCount
        dic["stateofshebao"] = user.stateOfSheBao
        dic["creditcard"] = user.creditCard
        dic["guarantee"] = user.guarantee
        dic["litigation"] = user.litigation
        dic["loan"] = user.loan
        dic["contactsoffriends"] = user.contactsOfFriends
        dic["investmentuse"] = user.investmentUse
        dic["onlineshoppingrecord"] = user.onlineShoppingRecord
        dic["personalcreditinquiry"] = user.personalCreditInquiry
        dic["wife"] = user.wife
        dic["estimatedloan"] = user.estimatedLoan
        dic["status"] = user.status
        
//        if(user.createtime == nil) {
//            dic["createtime"] = 0
//        } else {
//            dic["createtime"] = Int(user.createtime!.timeIntervalSince1970) * 1000
//        }
//        
//        if(user.updatetime == nil) {
//            dic["updatetime"] = 0
//        } else {
//            dic["updatetime"] = Int(user.updatetime!.timeIntervalSince1970) * 1000
//        }
//        
        //        print(dic)
        
        // 转换成json
        if (!JSONSerialization.isValidJSONObject(dic)) {
            print("无法解析出JSONString")
        }
        let data:NSData! = try? JSONSerialization.data(withJSONObject: dic, options: []) as NSData!
        let JSONString = NSString(data:data as Data,encoding: String.Encoding.utf8.rawValue)
        return JSONString! as String
    }
    
    
    /// 当前用户房产信息，转json
    ///
    /// - Returns: json 字符串
    func toJson(array:[House]) -> String{
        // 转换成字典数组
        var dics = [[String:Any]]()
        for house in array {
            var dic = [String:Any]()
            
            dic["id"] = house.id
            dic["userid"] = house.userid
            dic["numofcertificate"] = house.numOfCertificate
            dic["price"] = house.price
            dic["yearsofhouse"] = house.yearsOfHouse
            dic["marketvalue"] = house.marketValue
            dic["valueofarround"] = house.valueOfArround
            dic["cityofhouse"] = house.cityOfHouse
            dic["housetype"] = house.houseType
            dic["payment"] = house.payment
            dic["address"] = house.address
            dic["area"] = house.area
            dic["status"] = house.status
            
            
            dics.append(dic)
        }
        
//        print(dics)
        
        // 转换成json
        if (!JSONSerialization.isValidJSONObject(dics)) {
            print("无法解析出JSONString")
        }
        let data:NSData! = try? JSONSerialization.data(withJSONObject: dics, options: []) as NSData!
        let JSONString = NSString(data:data as Data,encoding: String.Encoding.utf8.rawValue)
        return JSONString! as String
    }

}
